package controllers

import (
	"github.com/gin-gonic/gin"
	"go.didapinche.com/foundation/apisix-dashboard/internal/app/models"
	"go.didapinche.com/foundation/apisix-dashboard/internal/app/services"
	"net/http"
)

type ServiceController struct {
	service services.Service
}

func NewServiceController(service services.Service) *ServiceController {
	return &ServiceController{service: service}
}

func (ctl ServiceController) Get(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	key := c.Param("id")
	u, err := ctl.service.Get(key, env.(string))
	if err != nil {
		c.String(http.StatusBadRequest, "ConsumerService Get failed:%v", err)
	}
	c.JSON(http.StatusOK, u)
}

func (ctl ServiceController) List(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	param := new(struct {
		Name     string `form:"name"`
		PageSize int    `form:"page_size"`
		PageNum  int    `form:"page_num"`
	})
	if err := c.ShouldBind(param); err != nil {
		c.String(http.StatusBadRequest, "bind params error:%v", err)
		return
	}
	us, err := ctl.service.List(param.Name, env.(string), param.PageSize, param.PageNum)
	if err != nil {
		c.String(http.StatusBadRequest, "ConsumerService List failed:%v", err)
	}
	c.JSON(http.StatusOK, us)
}

func (ctl ServiceController) Create(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	param := new(models.Service)
	if err := c.ShouldBind(param); err != nil {
		c.String(http.StatusBadRequest, "bind params error:%v", err)
		return
	}
	param.Name = param.ID
	err := ctl.service.Create(param, env.(string))
	if err != nil {
		c.String(http.StatusBadRequest, "ConsumerService Create failed:%v", err)
	}
}
func (ctl ServiceController) Update(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	param := new(models.Service)
	if err := c.ShouldBind(param); err != nil {
		c.String(http.StatusBadRequest, "bind params error:%v", err)
		return
	}
	param.Name = param.ID
	err := ctl.service.Update(param, env.(string))
	if err != nil {
		c.String(http.StatusBadRequest, "ConsumerService Update failed:%v", err)
	}
}

func (ctl ServiceController) Delete(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	key := c.Param("id")
	err := ctl.service.Delete(key, env.(string))
	if err != nil {
		c.String(http.StatusBadRequest, "ConsumerService Delete failed:%v", err)
	}
	c.JSON(http.StatusOK, "")
}

func (ctl ServiceController) GetName(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	name := c.Query("name")
	u, err := ctl.service.GetName(name, env.(string))
	if err != nil {
		c.String(http.StatusBadRequest, "ServiceController GetName failed:%v", err)
	}
	c.JSON(http.StatusOK, u)
}

func (ctl ServiceController) Syn(c *gin.Context) {
	param := new(struct {
		Service *models.Service `json:"service"`
		Env     []string        `json:"env"`
	})
	err := ctl.service.Syn(param.Service, param.Env)
	if err != nil {
		c.String(http.StatusBadRequest, "ServiceController Syn failed:%v", err)
	}
	c.JSON(http.StatusOK, "")
}
